$(function() {

    $("#start").autocomplete({
        source: "autocomplete_stops.php",
        minLength: 2,
        select: function(event, ui) {
            $('#start').val(ui.item.id);
        }
    });
});
$(function() {

    $("#end").autocomplete({
        source: "autocomplete_stops.php",
        minLength: 2,
        select: function(event, ui) {
            $('#end').val(ui.item.id);
        }
    });
});

var map;
var lineLayer;
// var osmSourceLayer;
var heat;
//intern Projection
var googleProjection = new OpenLayers.Projection("EPSG:900913");
//display Projection
var wgs84Projection = new OpenLayers.Projection("EPSG:4326");
var startlon = 7.60803868948409;
var startlat = 51.9401238659249;
var startZoom = 16;
var popup;
var clickedMarkerDescription;
var canvas;

function init() {
    map = new OpenLayers.Map("map", {

        controls: [new OpenLayers.Control.PanZoomBar(), new OpenLayers.Control.Navigation(), ],
        maxExtent: new OpenLayers.Bounds( - 20037508.34, -20037508.34, 20037508.34, 20037508.34),
        maxResolution: 156543.0399,
        numZoomLevels: 19,
        units: 'm',
        projection: googleProjection,
        displayProjection: wgs84Projection
    });

    var osmBackgroundLayer = new OpenLayers.Layer.OSM("Background", "http://tile.openstreetmap.org/mapnik/${z}/${x}/${y}.png", {
        numZoomLevels: 19
    });

    var geoRdfLayer = new GeoRdfLayer("RdfLayer");

    geoRdfLayer.icon = new OpenLayers.Icon('includes/Haltestelle_klein.png', new OpenLayers.Size(12, 12));

    map.addControl(new OpenLayers.Control.LayerSwitcher({
        'div': OpenLayers.Util.getElement('layerswitcher')
        }));

    //add all layers to map
    map.addLayers([osmBackgroundLayer, geoRdfLayer]);

    //set map center to a certain start position
    var startLonLat = new OpenLayers.LonLat(startlon, startlat).transform(wgs84Projection, map.getProjectionObject());
    map.setCenter(startLonLat, startZoom);
    lineLayer = new OpenLayers.Layer.Vector("Line Layer");
	
	console.log("init");
}

function drawPartOfLine(lat1, lon1, lat2, lon2) {
   if(map== null){
init();}
	console.log(lat1 + " " + lon1);
console.log(lat2 + " " + lon2);

    // map.addLayer(lineLayer);                    
    points = new Array();

    points[0] = new OpenLayers.LonLat(lon1, lat1).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());;
    points[0] = new OpenLayers.Geometry.Point(points[0].lon, points[0].lat);

    points[1] = new OpenLayers.LonLat(lon2, lat2).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());;
    points[1] = new OpenLayers.Geometry.Point(points[1].lon, points[1].lat);

    var line = new OpenLayers.Geometry.LineString(points);

    var style = {
        strokeColor: '#ff0000',
        strokeWidth: 5
    };

    var lineFeature = new OpenLayers.Feature.Vector(line, null, style);
    lineLayer.addFeatures([lineFeature]);


}

function drawLine() {
	if(map== null){
		init();
	}
    map.addLayer(lineLayer);
}

function hideSchedule() {
    document.getElementById("schedule").style.visibility = "hidden";
    document.getElementById("start").value = "";
    document.getElementById("end").value = "";
}

function asStart() {
    document.getElementById("start").value = clickedMarkerDescription;
}

function asStop() {
    document.getElementById("end").value = clickedMarkerDescription;
}

function startMap(){
	if(map==null){
		init();
	}
}

